//
//  AboutUsViewController.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/8/11.
//

import UIKit

class HTMLViewController: BaseViewController {
    
    init(HTMLString v2: String) {
        super.init(nibName: nil, bundle: nil)
        if let attri = App.shared.attributedItems[v2.MD5] {
            textView.attributedText = attri
        } else {
            activityIndicatorView.startAnimating()
            attributedStringFromHTML(v2) { [weak self] attributedString in
                self?.activityIndicatorView.stopAnimating()
                self?.textView.attributedText = attributedString
                self?.textView.textContainer.maximumNumberOfLines = 0
                self?.textView.textContainer.lineBreakMode = .byWordWrapping
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        titleLabel.text = "度一古琴"
        
        view.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.top.equalTo(naviHeaderView.snp.bottom)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(-view.safeAreaInsets.bottom)
        }
        
        view.addSubview(activityIndicatorView)
        activityIndicatorView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
    
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        textView.snp.updateConstraints { make in
            make.bottom.equalTo(-view.safeAreaInsets.bottom)
        }
    }
    
    private lazy var textView: UITextView = {
        let v = UITextView()
        v.isEditable = false
        v.isSelectable = false
        v.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        return v
    }()
    
    private let activityIndicatorView = UIActivityIndicatorView(style: .medium)
}
